package com.lakeworks.web.controller.core;


import com.lakeworks.common.core.domain.AjaxResult;
import com.lakeworks.common.domain.ticket.TktTicket;
import com.lakeworks.common.exception.response.ResponseException;
import com.lakeworks.domain.OprPickup;
import com.lakeworks.mapper.TktTicketMapper;
import com.lakeworks.service.IOprOrderService;
import com.lakeworks.service.IOprPickupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;
import java.util.List;
import java.util.Optional;

import static com.lakeworks.common.core.domain.AjaxResult.error;
import static com.lakeworks.common.core.domain.AjaxResult.success;

@RestController
@RequestMapping("/order")
@Validated
public class OprOrderController {

    @Resource
    private TktTicketMapper tktTicketMapper;

    @Autowired
    private IOprPickupService oprPickupService;

    @Autowired
    private IOprOrderService oprOrderService;

    /**
     * 订单详情页
     * */
    @GetMapping("/details")
    public AjaxResult getOrderDetails(@NotBlank(message = "订单号不能为空") String orderNo){
        Long channelId = Optional.ofNullable(oprPickupService.selectOprPickupList(OprPickup.builder().orderNo(orderNo).build()))
                .map(p-> p.get(0).getChannelId())
                .orElse(Optional.ofNullable(tktTicketMapper.selectTktTicketList(TktTicket.builder().orderNo(orderNo).build()))
                        .map(t-> t.get(0).getChannelId())
                        .orElse(null));
        if(channelId == null){
           return error("订单号错误，渠道ID为空");
        }
        return success(oprOrderService.getOrderDetails(channelId,orderNo));
    }

    /**
     * 物流详情
     * */
    @GetMapping("/express")
    public AjaxResult getOrderExpress(@NotBlank(message = "物流单号不能为空") String expressNo){
        return oprOrderService.getOrderExpress(expressNo);
    }

    /**
     * 获取出入厂单物流信息
     * */
    @GetMapping("/expressNos")
    public AjaxResult getOrderExpressNos(@NotBlank(message = "订单号不能为空") String orderNo){
        return oprOrderService.getOrderExpressNos(orderNo);
    }




}
